package com.ruoyi.system.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.system.constants.redisConstants;
import com.ruoyi.system.domain.dtos.HotVideoDto;
import com.ruoyi.system.service.IHotVideoService;
import com.ruoyi.system.service.impl.HotVideoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysVideo;
import com.ruoyi.system.service.ISysVideoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 视频Controller
 * 
 * @author ruoyi
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/video")
public class SysVideoController extends BaseController
{
    @Autowired
    private ISysVideoService sysVideoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IHotVideoService hotVideoService;


    /**
     * 获取热文章
     * @return
     */
    @PostMapping("/hotVideo")
    public AjaxResult getHotVideos(@RequestBody SysVideo sysVideo){
        // 更新热文章
        hotVideoService.hotFresh();
        stringRedisTemplate.opsForValue().set("HotFresh", JSON.toJSONString(LocalDateTime.now()));
        String hotVideoJson = stringRedisTemplate.opsForValue().get("hot-"+sysVideo.getChannelId());
        List<HotVideoDto> hotVideoDto = JSON.parseArray(hotVideoJson, HotVideoDto.class);
        return AjaxResult.success(hotVideoDto);
    }

    /**
     * 查询视频列表
     */
    @RequiresPermissions("system:video:list")
    @GetMapping("/list")
    public TableDataInfo list(SysVideo sysVideo)
    {
        startPage();
        List<SysVideo> list = sysVideoService.selectSysVideoList(sysVideo);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @RequiresPermissions("system:video:export")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVideo sysVideo)
    {
        List<SysVideo> list = sysVideoService.selectSysVideoList(sysVideo);
        ExcelUtil<SysVideo> util = new ExcelUtil<SysVideo>(SysVideo.class);
        util.exportExcel(response, list, "视频数据");
    }

    /**
     * 获取视频详细信息
     */
    @RequiresPermissions("system:video:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysVideoService.selectSysVideoById(id));
    }

    /**
     * 新增视频
     */
    @RequiresPermissions("system:video:add")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVideo sysVideo)
    {
        return toAjax(sysVideoService.insertSysVideo(sysVideo));
    }

    /**
     * 新增视频并返回id
     * @param sysVideo
     * @return
     */
    @PostMapping("/addAndReturnVideoId")
    public AjaxResult addAndReturnVideoId(@RequestBody SysVideo sysVideo)
    {
        sysVideoService.insertSysVideo(sysVideo);
        return AjaxResult.success(sysVideo.getId());
    }

    /**
     * 修改视频
     */
    @RequiresPermissions("system:video:edit")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVideo sysVideo)
    {
        return toAjax(sysVideoService.updateSysVideo(sysVideo));
    }

    /**
     * 删除视频
     */
    @RequiresPermissions("system:video:remove")
    @Log(title = "视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysVideoService.deleteSysVideoByIds(ids));
    }
}
